home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / Prograph Reference Manual.sit / Prograph Reference Manual / Prograph Reference 8-End.rsrc / TEXT_137.txt < prev    next >
Text File  |  1995-10-26  |  14KB  |  397 lines

  1.  
  2. t Using the Datafile Manager
  3.  
  4. This section explains the procedures to follow when creating a datafile using the Prograph Datafile Manager. Please note that information about specific Datafile Manager primitives can be found at the end of this chapter.  *475*
  5.  
  6. High Level Overview  *476*
  7.  
  8. The following diagram shows the basic steps involved in creating and maintaining a datafile using the Prograph Datafile Manager. Each step is explained in detail below:
  9.  
  10. ツ 
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. _____________________________________________________
  37. CAUTION: 
  38. Table, and Key names are case-sensitive.
  39. -----------------------------------------------------
  40.  
  41. Loading Datafile primitives  *476*
  42.  
  43. Datafile primitives are stored in the Datafile Primitives file.  This file is located in the Disabled Primitives folder, a subfolder within the Prograph Extensions folder.  These primitives are not initially loaded with Prograph Classic, as packaged.  In order to use the datafile primitives, shut down Prograph, move the Datafile Primitives file into the Prograph Extensions folder, and add 200K to Prograph's minimum memory allocation using the Get Info dialog.  The datafile primitives will be available the next time you start Prograph.
  44.  
  45. Creating A New Datafile  *477*
  46.  
  47. Every datafile must contain at least one table. Likewise, tables contain clusters which are indexed by keys. Therefore, creating a new datafile involves the following steps:
  48.  
  49. 1.    Create a datafile.
  50.  
  51. 2.    Create a table (at least one) in the datafile.
  52.  
  53. 3.    Create a key for the table (a table can have several keys).
  54.  
  55. These three steps are described in detail in the following sections.
  56.  
  57. How to Create a Datafile  *477*
  58.  
  59. Datafiles are created by calling the db-new primitive. This primitive must be passed the name of the data file you wish to create. db-new creates a new data file on your disk and return an error number (indicating whether or not the primitive succeeded) and an id number which you will need later for creating tables within the datafile. The method below shows how to create a datafile called My Database:
  60.  
  61. ツ 
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. The DB id# parameter returned by db-new is needed later for creating tables and for closing the data file, therefore you should store the DB id#  so that you may use it when necessary. The example above stores the DB id# in a persistent.
  71.  
  72. _________________________________________________________
  73. Note that the filename supplied to the primitive db-new (窶廴y database窶) in the previous example could also contain the path of the file (e.g. 窶廩D80:Datafolder:My Database窶).
  74. --------------------------------------------------------
  75.  
  76. How to Create a Table  *478*
  77.  
  78. Tables are created by calling the table-new primitive. This primitive must be supplied with the following:
  79.  
  80. r the database file id (DB id# from previous example), and
  81.  
  82. r  a name for the table.
  83.  
  84. table-new will create a new table within a datafile, and return an error # (indicating whether or not the primitive succeeded) and a table id# which you will need later for creating keys and writing clusters within the table. The following method shows how to create a table called Invoices:
  85.  
  86. ツ 
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. How to Create a Key for the Table  *478*
  96.  
  97. Keys are created by calling the key-new primitive. This primitive must be supplied with the following:
  98.  
  99. r the table id (Table id# from the previous example), and
  100.  
  101. r a name for the key.
  102.  
  103. key-new will create a new key for a table and return an error #(indicating whether or not the primitive succeeded) and a key id #. The following method shows how to create a key named Customer Number:
  104.  
  105. ツ 
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. ________________________________________________________
  115. IMPORTANT!
  116.  
  117. Creating a datafile, table or key with the respective primitives (db-new, table-new, or key-new) will also open the item (datafile, table or key) after creating it. However, any newly-created item cannot be used for storing data until it has been closed and then reopened.
  118.  
  119. For example, while table-new will create a new table within a datafile and then open it, the table id# for this newly created table can only be used for creating new keys 窶“ it cannot be used for writing clusters of data to the table. If you want to write data to the table, you must use the table-id# generated by the table-open primitive, as discussed in the next section on Opening Datafiles, Tables, and Keys.
  120. --------------------------------------------------------
  121.  
  122. Sample Method for Creating a Datafile  *479*
  123.  
  124. ツ 
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. Opening Datafiles, Tables & Keys
  137.  
  138. Opening A Datafile  *479*
  139.  
  140. A datafile may be opened in any of three different modes 窶“ Query, Update or Shared. Which mode you choose depends on what you intend to do with the datafile once it is opened.
  141.  
  142. A datafile opened in Query mode can be read, but new data cannot be written to the datafile. Query mode effectively means read-only mode. Multiple users may simultaneously open and read a datafile in Query mode.
  143.  
  144. A datafile opened in Update mode may be read and written to by a single user. This user may read data, write new data to the datafile, delete clusters of data or change the structure (tables and keys) of the datafile. Update mode effectively means read-write single-user mode.
  145.  
  146. A datafile opened in Shared mode can be read or written to by multiple users. A datafile opened in Shared mode can be opened by several users simultaneously and all users may read and write to the datafile. Shared mode effectively means read-write multi-user mode.
  147.  
  148. Since a datafile contains tables and the tables usually contain keys, the process of opening a datafile involves the following steps:
  149.  
  150. 1.    Open the datafile
  151.  
  152. 2.    Open the table(s) in the datafile
  153.  
  154. 3.    Open the key(s) for the table(s)
  155.  
  156. How to Open a Datafile  *480*
  157.  
  158. Datafiles are opened by calling the db-open primitive. This primitive must be supplied with the following:
  159.  
  160. r the mode in which to open the datafile, and
  161.  
  162. r the name of the datafile that you wish to open. 
  163.  
  164. db-open will open an existing datafile and return an error # (indicating whether or not the primitive succeeded) and an id number which you will need later for opening tables within the datafile. The following method show show to open a datafile named My Database, in Update mode:
  165.  
  166. ツ 
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175. The DB id# parameter returned by db-open will be needed later for opening tables and also for closing the datafile, so you should store the DB id# somewhere so that you may use it when necessary. The example above stores the DB id# in a persistent.
  176.  
  177. How to Open a Table  *481*
  178.  
  179. Tables are opened by calling the table-open primitive. This primitive must be supplied with the following:
  180.  
  181. r the database id number (DB id# from the previous example), and
  182.  
  183. r the name of the table.
  184.  
  185. table-open will open an existing table within a datafile and return an error # (indicating whether or not the primitive succeeded) and a table id# which you will need later for opening keys and reading & writing clusters within the table.
  186.  
  187. The following method opens a table named Invoices:
  188.  
  189. ツ 
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199. The table id# parameter returned by table-open will be needed later for open keys and also for reading and writing clusters in the table, therefore you should store the table id# somewhere so that you may use it when necessary. The example above stores the table id# in a persistent. 
  200.  
  201. How to Open a Key  *481*
  202.  
  203. Keys are opened by calling the key-open primitive. This primitive must be supplied with the following:
  204.  
  205. r the table id number (Table id# from the previous example), and
  206.  
  207. r the name of the key. 
  208.  
  209. key-open will open an existing key within a table and return an error # (indicating whether or not the primitive succeeded) and a key id# which you will need later for finding clusters based on key values.The following method opens a key named customer number:
  210.  
  211. ツ 
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221. The key id# parameter returned by key-open will be needed later for setting values in the key field and also for finding clusters based on key values, therefore you should store the key id# somewhere so that you may use it when necessary. The example above stores the key id# in a persistent.
  222.  
  223. The following example opens a datafile in Update mode. The datafile窶冱 table and the table窶冱 key are also opened. The id numbers associated with each are stored in persistents so that they may be used later. Data can now be read and stored in the datafile窶冱 table.
  224.  
  225. Sample Method for Opening a Datafile  *482*
  226.  
  227. ツ 
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241. Finding/Navigating Clusters  *482*
  242.  
  243. Clusters are the basic data objects stored in a datafile. They are analogous to a single unit of data or a record.
  244.  
  245. Keys are indexed fields associated with clusters. Key values are the data stored in the key field.
  246.  
  247. The purpose of a key is to provide a means for indexed, random access to clusters of data within a datafile. 
  248.  
  249. u To locate and access a particular cluster, provide a specific data value to the key-read or key-find primitive in your method, as described in How to Find a Cluster, below.
  250.  
  251. u To navigate or step through clusters of a datafile, use the key-first, key-last, key- next, and key-previous primitives.
  252.  
  253. How to Find a Cluster  *483*
  254.  
  255. The Datafile Manager provides several ways to find clusters. Which method is used depends on what you intend to do with the cluster once you窶况e found it.
  256. There are primitives available that allow you to navigate all clusters in a datafile.  cluster-first returns the id of the first cluster in the datafile, and cluster-next returns the id of the next cluster.
  257.  
  258. In many circumstances, you may simply want to read in the cluster窶冱 data. In such a case, find the cluster by calling the key-read primitive. This primitive must be supplied with the following:
  259.  
  260. r the key id number (Key id# from a previous example), and
  261.  
  262. r the value contained in the desired cluster窶冱 key. 
  263.  
  264. The key-read primitive will locate the appropriate cluster (if one exists) and return an error number (indicating whether or not a cluster was found) as well as the cluster窶冱 data.
  265.  
  266. The following method finds a cluster with a key field of 100, and returns the cluster窶冱 data:
  267.  
  268. In some circumstances, you may wish to find the cluster so that you can take some kind of action beyond simply reading the cluster, such as deleting the cluster or overwriting its data. In this case, you must find the cluster and obtain the cluster id number, which is done by using the key-find primitive and the key-value primitive. The key-find primitive must be supplied with the following:
  269.  
  270. r the key id number (Key id# from a previous example), and
  271.  
  272. r the value of the key field for the required cluster. 
  273.  
  274. It will locate the appropriate cluster and return an error number (indicating whether or not a cluster was found). Then the key-value primitive is called, to retrieve the cluster id number. 
  275.  
  276. The key-value primitive must be supplied with the key id number (Key id# from a previous example). It reads the key value of the key field and the cluster id number of the cluster. 
  277.  
  278. The following sample method finds a cluster with a key field of 100 (by calling key-find) and then retrieves the cluster id number (by calling key-value):
  279.  
  280. ツ 
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290. Reading Clusters  *484*
  291.  
  292. How to Read a Cluster
  293.  
  294. The Datafile Manager provides two ways to read in a cluster窶冱 data. The method you use depends on how you intend to select the cluster to be read, as follows:
  295.  
  296. u To find a cluster based on a key value, use the key-read primitive, which will find and read the cluster. Please refer to the first example in the section, How to Find a Cluster.
  297.  
  298. u If you are stepping through the clusters sequentially using the key-next, key-previous, key-first or key-last primitives, or if you have already found the desired cluster by some other means, then you can read in the cluster窶冱 data by calling the cluster-read primitive. 
  299.  
  300. The cluster-read primitive must be supplied with the following:
  301.  
  302. r the table id number, and
  303.  
  304. r the cluster id number 
  305.  
  306. To retrieve a cluster窶冱 id number, call the key-value primitive. 
  307.  
  308. cluster-read reads the data stored in a cluster into memory, and returns an error number (indicating whether or not the primitive succeeded) as well as the data of the cluster.
  309.  
  310. The following method reads in the next cluster:
  311.  
  312. ツ 
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325. Writing Clusters  *485*
  326.  
  327. Adding a New Cluster to a Table
  328.  
  329. To add a new cluster to a table within a datafile, call the cluster-write primitive. This primitive must be supplied with the following:
  330.  
  331. r the table id number (Table id # from the previous example) of the table to which the cluster is to be added,
  332.  
  333. r the data to be stored within the cluster, and
  334.  
  335. r a list of key values for the cluster (the key value list should be in alphabetical order by key name);
  336.  
  337. cluster-write adds a new cluster to a table within a datafile and returns an error number (indicating whether or not the primitive succeeded) and the id number of the new cluster.
  338.  
  339. The following method adds a new cluster (containing a list of two strings) to a table, and sets the cluster窶冱 key field to 100:
  340.  
  341. ツ 
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349. Overwriting an Existing Cluster  *486*
  350.  
  351. To replace old data stored in a cluster by new data, call the cluster-replace primitive. This primitive must be supplied with the following:
  352.  
  353. r the table id number,
  354.  
  355. r the cluster id number, and
  356.  
  357. r the new data for the cluster.
  358.  
  359. cluster-replace replaces a cluster within a table and returns an error number (indicating whether or not the primitive succeeded) and the new cluster id number.
  360.  
  361. The following method replaces an old cluster (that has a key of 100) with a new cluster:
  362.  
  363. ツ 
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376. Closing Datafiles  *486*
  377.  
  378. Although it is possible to close tables and keys as well as datafiles, it is not usually necessary to close a table or a key, since all tables and keys are closed when a datafile is closed.
  379.  
  380. How to Close a Datafile
  381.  
  382. To close a datafile, call the db-close primitive. This primitive must be supplied with the following:
  383.  
  384. r the id number of the datafile to be closed.
  385.  
  386. db-close closes a datafile and all of the datafile窶冱 tables and keys, and returns an error number to indicate whether or not the primitive succeeded.
  387.  
  388. The following method closes a datafile:
  389.  
  390. ツ 
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.